'use strict';

export default function transformResponseData(config = {}) {
	return next => action => {

		const {
			type, 
			payload = {}, 
			meta, 
			onSuccess, 
			onError, 
			onFinish
		} = action;

		if (/_PENDING$/.test(type)) {
			return next({
				type,
				state: 1,
					isError: false
			});
		}

		const {
			Code, Data, Msg
		} = payload;
		let _data = {};

		if (Code === 200) {
			_data = {
				type,
				state: 2,
				isError: false,
				data: Data
			}
			onSuccess && onSuccess(payload, Data);
		} else {
			_data = {
				type,
				state: 4,
				isError: true,
				code: Code,
				msg: Msg,
				data: payload
			}
			onError && onError(payload);
		}
		onFinish && onFinish(payload);

		return next(_data);

	}
}